package com.ctaa.ctaa.serviceimpl;

import com.ctaa.ctaa.dto.Result;
import com.ctaa.ctaa.mapper.AnalysisMapper;
import com.ctaa.ctaa.mapper.BkkpMapper;
import com.ctaa.ctaa.pojo.AnalysisPojo;
import com.ctaa.ctaa.pojo.BkkpPojo;
import com.ctaa.ctaa.service.BkkpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 记账
 */
@Service
public class BkkpServiceImpl implements BkkpService {

    @Resource
    BkkpMapper bkkpMapper;
    @Resource
    AnalysisMapper analysisMapper;

    @Override
    public Result<Boolean> bk(BkkpPojo bkkpPojo) {
        Integer integer = bkkpMapper.insertBkkp(bkkpPojo);
        if (integer == 1) {
            return Result.success("记账成功");
        }
        return Result.fail("记账入库失败");
    }

    @Override
    public Result<Map<String, Object>> searchList(String beginTime, String endTime, Integer type,
                                                  Integer userId, Integer limit, Integer offset) {
        if (userId == null) {
            return Result.fail("登录验证过期，请重新登陆");
        }
        Map<String, Object> map = new HashMap<>();
        //查询总支出
        Double aDouble2 = analysisMapper.queryExpSumMnoey(userId);
        //查询总收入
        Double aDouble3 = analysisMapper.queryImpSumMnoey(userId);
        double v = aDouble3 - aDouble2;//结余
        AnalysisPojo analysisPojo = new AnalysisPojo();
        analysisPojo.setIncome(aDouble3);
        analysisPojo.setSurplus(v);
        analysisPojo.setNsume(aDouble2);


        map.put("total", analysisPojo);
        List<BkkpPojo> query = bkkpMapper.query(beginTime, endTime, type, userId, limit, offset);
        map.put("data", query);
        return Result.success("查询成功", map);
    }


}
